/**
 * 多维对象转一位
 */

export function ignoreLoopLimit() {
    const e = window.onerror
    window.onerror = function (err) {
        if (err === 'ResizeObserver loop limit exceeded') {
            console.warn('Ignored: ResizeObserver loop limit exceeded')
            return false
        } else {
            return e(...arguments)
        }
    }
}


/**
 * 对象数据扁平化
 */
export function jsonFlatten(data) {
    var result = {}
    function recurse(cur, prop) {
        if (Object(cur) !== cur) {
            result[prop] = cur
        } else if (Array.isArray(cur)) {
            for (var i = 0, l = cur.length; i < l; i++) { recurse(cur[i], prop + '[' + i + ']') }
            if (l === 0) { result[prop] = [] }
        } else {
            var isEmpty = true
            for (var p in cur) {
                isEmpty = false
                recurse(cur[p], p)
            }
            if (isEmpty && prop) { result[prop] = {} }
        }
    }
    recurse(data, '')
    return result
}

/**
 * 
 * @param {*} obj 传入需要实现扁平化的对象或数组
 * @returns 
 */
export function test(obj) {
    const res = {}
    function _test(obj, prev = null) {
        // 数组
        if (Array.isArray(obj)) {
            for (const index in obj) {
                const val = obj[index]
                if (val instanceof Object) {
                    _test(val, `${prev ? prev : +''}[${index}]`)
                } else {
                    res[`${prev ? prev : +''}[${index}]`] = val
                }
            }
            return
        }

        // 对象
        for (const key in obj) {
            if (typeof obj[key] === 'object') {
                // null
                if (obj[key] !== null) {
                    _test(obj[key], `${prev ? prev + '.' : ''}${key} `)
                }
            } else {
                // undefined
                if (obj[key] !== undefined) {
                    res[`${prev ? prev + '.' : ''}${key} `] = obj[key]
                }
            }
        }
    }
    _test(obj)
    return res
}

/**
 * 
 * @param {*} obj 
 * @param {*} keys 
 * @returns 
 * 对象筛选属性
 */
export function filterObjKeys(obj, keys) {
    return Object.keys(obj).reduce((newData, key) => {
        if (keys.includes(key)) {
            newData[key] = obj[key];
        }
        return newData;
    }, {});
}
/**
 * 
 * @param {*} d 
 * @returns 
 * 把Mon Aug 01 2022 00:00:00 GMT+0800 (中国标准时间)格式的日期转换为yyyy-MM-dd HH:mm:ss
 */
export function formatDate(d) {
    var date = new Date(d);
    var YY = date.getFullYear() + '-';
    var MM =
        (date.getMonth() + 1 < 10
            ? '0' + (date.getMonth() + 1)
            : date.getMonth() + 1) + '-';
    var DD = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
    var hh =
        (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
    var mm =
        (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) +
        ':';
    var ss =
        date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
    return YY + MM + DD + ' ' + hh + mm + ss;
}
/**
 * 
 * @param {*} fn 事件
 * @param {*} delay 时间
 * @returns 
 * 防抖
 */
export function debounce(fn, delay) {
    let timer = null;
    return function () {
        clearTimeout(timer);
        timer = setTimeout(() => {
            fn.apply(this, arguments);
        }, delay)
    }
}

/**
 * 
 * @param {*} fn 事件
 * @param {*} delay 时间
 * @returns 
 * 节流
 */
export function throttle(fn, delay) {
    let valid = true;
    return function () {
        if (valid) { //如果阀门已经打开，就继续往下
            setTimeout(() => {
                fn.apply(this, arguments);//定时器结束后执行
                valid = true;//执行完成后打开阀门
            }, delay)
            valid = false;//关闭阀门
        }
    }
}
/**
 * 
 * @param {*} array 要筛选的数组
 * @returns 
 * 筛选为tree的数组的方法
 */
export const dealTreeData = (array) => {
    const data = array.map((item) => ({
        label: item.name,
        id: item.id,
        children: item.children && item.children.length ? dealTreeData(item.children) : null
    }))
    return data
}

export const changeTableHead = (tableData, fieldName) => {
    const list = tableData.map((item) => {
        const obj = {};
        for (const k in item) {
            if (fieldName[k]) {
                obj[fieldName[k]] = item[k];
            }
        }
        return obj;
    });
    return list;
}

//对象转数组的方法
export function objToArr(obj) {
    let arr = []
    for (let key in obj) {
        arr.push(obj[key])
    }
    return arr
}

let util = {};
// 对象扁平化处理
const objectFlat = function (obj) {
    let res = {};
    let res2 = {};
    Object.keys(obj).forEach((i) => {
        if (obj[i] instanceof Object) {
            res2 = objectFlat(obj[i]);
        } else {
            res[i] = obj[i];
        }
    });
    return {
        ...res,
        ...res2,
    };
};

export default objectFlat
